import { loginApi, getInfoApi, getDetailUserInfoApi } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
export default {
  namespaced: true,
  state: {
    token: getToken(),
    userInfo: {}
  },
  mutations: {
    setToken(state, payload) {
      state.token = payload
      setToken(payload)
    },
    removeToken(state) {
      state.token = ''
      removeToken()
    },
    setUserInfo(state, payload) {
      state.userInfo = payload
    },
    removeUserInfo(state) {
      state.userInfo = {}
    }
  },
  actions: {
    async loginActions(context, data) {
      const res = await loginApi(data)
      console.log(res)
      context.commit('setToken', res)
    },
    async getInfoApiActions(context, data) {
      const res = await getInfoApi()
      console.log(res)
      const res2 = await getDetailUserInfoApi(res.userId)
      console.log(res2)
      context.commit('setUserInfo', {...res,...res2})
    },
    quitAction(context) {
      context.commit('removeToken')
      context.commit('removeUserInfo')
    }
  }
}

